<?php
//=======================================
//###################################
// Kayako Web Solutions
//
// Source Copyright 2001-2004 Kayako Web Solutions
// Unauthorized reproduction is not allowed
// License Number: $%LICENSE%$
// $Author: mslaria $ ($Date: 2007/02/27 14:03:33 $)
// $RCSfile: client_misc.php,v $ : $Revision: 1.8 $ 
//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
//
//###################################
//=======================================


if (!defined("INSWIFT")) {
	trigger_error("Unable to process $PHP_SELF", E_USER_ERROR);
}

require_once ("./includes/functions_users.php");
require_once ("./includes/functions_customfields.php");
require_once ("./includes/functions_captcha.php");

$template->loadLanguageSection("misc");

// ======= PERMISSION CHECK =======
if ($_SWIFT["user"]["permissions"]["perm_canviewregister"] == "0")
{
	$template->assign("errormessage", $_SWIFT["language"]["perminvalid"]);
	echo $template->displayTemplate("header");
	echo $template->displayTemplate("footer");
	exit;
}

/**
* ###############################################
* REGISTER / LOST PASSWORD
* ###############################################
*/
if ($eventaction == "register")
{
	$template->assign("sessionid", $_SWIFT["session"]["sessionid"]);

	if ($_GET["do"] == "captcha")
	{
		generateCaptcha();
		exit;
	}

	unset($displaytemplate);

	if ($_POST["step"] == 1)
	{
		if (trim($_POST["fullname"]) == "" || trim($_POST["email"]) == "" || trim($_POST["password"]) == "" || trim($_POST["passwordagain"]) == "")
		{
			$template->assign("errormessage", $_SWIFT["language"]["requiredfieldempty"]);			
		} else if (trim($_POST["password"]) != "" && $_POST["password"] != $_POST["passwordagain"]) {
			$template->assign("errormessage", $_SWIFT["language"]["passworddontmatch"]);
		} else if (!isValidEmail($_POST["email"])) {
			$template->assign("errormessage", $_SWIFT["language"]["invalidemail"]);
		} else if ($_SWIFT["session"]["captcha"] != $_POST["verifyregcode"]) {
			$template->assign("errormessage", $_SWIFT["language"]["invalidverificationcode"]);
		} else if (!checkCustomFieldContent(CUSTOMFIELD_USER)) {
			$template->assign("errormessage", $_SWIFT["language"]["requiredfieldempty"]);
		} else {
			// Go! Go! Go!, Register the user!! :D
			if ($_SWIFT["settings"]["user_enableemailverification"] == 1 || $_SWIFT["settings"]["u_enablesveri"] == 1)
			{
				$enabled = 0;
			} else {
				$enabled = 1;
			}
			$userid = insertUser($enabled, $_POST["email"], $_POST["password"], $_SWIFT["tgroup"]["regusergroupid"], LOGINAPI_DEFAULT, 0, $_POST["fullname"], $_SWIFT["languageid"], 0, false, 1);
			if (!$userid)
			{
				$template->assign("errormessage", $_SWIFT["language"]["emailalreadyexists"]);
			} else {
				updateCustomFields(CUSTOMFIELD_USER, $userid);
				$dotemplate = iif($enabled==1,"registersuccess", "registervalidate");
			}
		}
	} else if ($_GET["do"] == "va") {
		// Try to get the verify hash from the db..
		$_userverifyhash = $dbCore->queryFetch("SELECT * FROM `". TABLE_PREFIX ."userverifyhash` WHERE `userverifyhashid` = '". $dbCore->escape($_GET["hash"]) ."';");
		if (!empty($_userverifyhash["userverifyhashid"]) && $_GET["hash"] == $_userverifyhash["userverifyhashid"])
		{
			// Ok hash is ok, enable the user.
			$dbCore->query("UPDATE `". TABLE_PREFIX ."users` SET `enabled` = '1' WHERE `userid` = '". intval($_userverifyhash["userid"]) ."';");
			$dbCore->query("DELETE FROM `". TABLE_PREFIX ."userverifyhash` WHERE `userverifyhashid` = '". $dbCore->escape($_GET["hash"]) ."';");
			$dotemplate = "registersuccess";
			$_user = sendRegistrationEmail($_userverifyhash["userid"]);
			$_POST["fullname"] = $_user["fullname"];
			$_POST["email"] = $_user["email"];
			$nocustomconfirm = true;
		} else {
			// Invalid Hash
			$template->assign("errormessage", $_SWIFT["language"]["invaliduserhash"]);
		}
	}

	$template->cache(array('register', 'header', 'footer', 'navbar', 'customfields', 'customfieldconfirmstatic', 'registersuccess', 'registerpending'));
	$template->assign("navigation", '<a href="index.php" id="navlink">'.$_SWIFT["language"]["navhome"].'</a> &raquo; <a href="index.php?_m=core&_a=register" id="navlink">'.$_SWIFT["language"]["register"]."</a>");

	$template->assign("backurl", "index.php");
	$template->assign("fullname", htmlspecialchars($_POST["fullname"]));
	$template->assign("email", htmlspecialchars($_POST["email"]));
	$template->assign("randomhash", buildHash());

	if (canCaptcha() && $_SWIFT["settings"]["user_enablecaptcha"] == 1)
	{
		generateCaptchaWord();
		$template->assign("cancaptcha", "1");
	} else {
		$template->assign("cancaptcha", "0");
	}

	if ($dotemplate == "registersuccess")
	{
		if ($nocustomconfirm != true)
		{
			$cfrender = renderClientStaticCustomFields(CUSTOMFIELD_USER);
		}
		$template->assign("cfrender", $cfrender);
		$template->assign("sectiontitle", $_SWIFT["language"]["registrationsuccess"]);
		$template->assign("sectiondesc", $_SWIFT["language"]["registrationsuccessdesc"]);
		echo $template->displayTemplate("registersuccess");
	} else if ($dotemplate == "registervalidate") {
		$cfrender = renderClientStaticCustomFields(CUSTOMFIELD_USER);
		$template->assign("cfrender", $cfrender);
		$template->assign("sectiontitle", $_SWIFT["language"]["regvalidpending"]);
		$template->assign("sectiondesc", iif($_SWIFT["settings"]["u_enablesveri"]==1, $_SWIFT["language"]["regvalidpendingdescman"], $_SWIFT["language"]["regvalidpendingdesc"]));
		echo $template->displayTemplate("registervalidate");
	} else {
		$cfrender = renderClientCustomFields(CUSTOMFIELD_USER);
		$template->assign("cfrender", $cfrender);
		$template->assign("sectiontitle", $_SWIFT["language"]["register"]);
		$template->assign("sectiondesc", $_SWIFT["language"]["registerdesc"]);
		echo $template->displayTemplate("register");
	}
}
?>